package com.excel.write.base.impl;

import com.excel.write.ExcelContent;
import com.excel.write.base.BaseExcelWrite;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 输出到文件
 */
public class ExcelWriteFile extends BaseExcelWrite {
    private String cacheDir;
    private String fileName;

    ExcelWriteFile(String cacheDir, String fileName) {
        super(fileName);
        this.cacheDir = cacheDir;
        this.fileName = fileName;

    }

    public static void write(String cacheDir, String fileName, List<ExcelContent> excelContents) {
        ExcelWriteFile excelWrite = new ExcelWriteFile(cacheDir, fileName);
        excelWrite.write(excelContents);
    }

    @Override
    protected void write(Workbook workbook) {
        try {
            File file = new File(cacheDir, fileName);
            FileOutputStream out = new FileOutputStream(file);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
